package ciyuan.Item.Material;

import ciyuan.Block.BlockRegistryManager;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.MaterialType;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class StonePot extends MaterialType {


    public StonePot() {
        super(new Properties().tab(ItemGroups.Material));
    }

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack itemstack = playerEntity.getItemInHand(hand);
        if (!world.isClientSide()) {
            RayTraceResult raytraceresult = getPlayerPOVHitResult(world, playerEntity,  RayTraceContext.FluidMode.SOURCE_ONLY);
            BlockRayTraceResult blockraytraceresult = (BlockRayTraceResult)raytraceresult;
            if (raytraceresult.getType() == RayTraceResult.Type.BLOCK) {
                BlockPos blockpos = blockraytraceresult.getBlockPos();
                Direction direction = blockraytraceresult.getDirection();
                BlockPos blockpos1 = blockpos.relative(direction);
                if (world.mayInteract(playerEntity,blockpos) && playerEntity.mayUseItemAt(blockpos1,direction,itemstack)) {
                    if (world.getBlockState(blockpos).getBlock().getRegistryName().getPath().toLowerCase().contains("water")) {
                        itemstack = new ItemStack(ItemRegisterManager.stonepot2.get());
                        world.setBlock(blockpos, Blocks.AIR.defaultBlockState(), 3);
                        return ActionResult.fail(itemstack);
                    } else if (world.getBlockState(blockpos).getBlock().is(BlockRegistryManager.oil.get())) {
                        itemstack = new ItemStack(ItemRegisterManager.stonepot3.get());
                        world.setBlock(blockpos, Blocks.AIR.defaultBlockState(), 3);
                        return ActionResult.fail(itemstack);
                    }
                }
            }
        }
        return ActionResult.pass(itemstack);
    }


}
